package com.hsc.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsc.commonutils.R;
import com.hsc.staservice.client.UcenterClient;
import com.hsc.staservice.entity.StatisticsDaily;
import com.hsc.staservice.mapper.StatisticsDailyMapper;
import com.hsc.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-20
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        //远程调用某一天注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        //把获取数据添加到数据
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);//统计人数
        sta.setDateCalculated(day);//统计日期
        //生成随机数模拟数据，这些数据日后在完善，
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));//完善需要查视频播放表，暂时没有
        sta.setLoginNum(RandomUtils.nextInt(100,200));//完善需要查登录日志表，暂时没有
        sta.setCourseNum(RandomUtils.nextInt(100,200));//完善需要查课程表
        baseMapper.insert(sta);

    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);//查询字段选择
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        //返回有两部分数据，日期和日期对应数量如注册人数、播放数量等
        //前端要求数组json结构
        //创建两个list集合，一个存放日期，一个存放数量，根据下标位置呈现一一对应关系
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        //遍历结果list集合，进行封装
        for (StatisticsDaily statisticsDaily : staList) {
            date_calculatedList.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculated",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
